package a_new_data_structure.tree.binary_tree;

import java.util.ArrayList;
import java.util.List;

public class BinaryTree {

    private static class TreeNode {
        int data;
        TreeNode left;
        TreeNode right;

        TreeNode(int data) {
            this.data = data;
        }

        TreeNode(int data, TreeNode left, TreeNode right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }
    public TreeNode root;

    public BinaryTree(int[] arr) {

    }

    public BinaryTree() {
        root = createTree();
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(3);
        TreeNode D = new TreeNode(4);
        TreeNode E = new TreeNode(5);
        TreeNode F = new TreeNode(6);
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.right = F;
        return A;
    }

    public void preOrder() {
        System.out.print("[ ");
        preOrder(root);
        System.out.println("]");
    }

    public void preOrder(TreeNode root) {
        if (root == null) {
            System.out.print("null ");
            return;
        }
        System.out.print(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder() {
        System.out.print("[ ");
        inOrder(root);
        System.out.println("]");
    }

    public void inOrder(TreeNode root) {
        if (root == null) {
            System.out.print("null ");
            return;
        }
        inOrder(root.left);
        System.out.print(root.data + " ");
        inOrder(root.right);
    }

    public void postOrder() {
        System.out.print("[ ");
        postOrder(root);
        System.out.println("]");
    }

    public void postOrder(TreeNode root) {
        if (root == null) {
            System.out.print("null ");
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.data + " ");
    }

    public List<Integer> preOrderToList() {
        List<Integer> list = new ArrayList<>();
        preOrderToList(root, list);
        return list;
    }

    public void preOrderToList(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        list.add(root.data);
        preOrderToList(root.left, list);
        preOrderToList(root.right, list);
    }
}
